from django.test import TestCase
from sign.models import Event, Guest
from django.contrib.auth.models import User
from django.contrib import auth


# Create your tests here.
class ModelTest(TestCase):

    def setUp(self):
        Event.objects.create(id=1, name='五一节可否一战？', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Guest.objects.create(id=1, event_id=1, realname='王斌', phone='15306180002', email='wangbing@mail.com',
                             sign=False)

    def test_event_models(self):
        result = Event.objects.get(name='五一节可否一战？')
        self.assertEqual(result.address, "金山度假村石化二村村长寝室")
        self.assertFalse(result.status)

    def test_guest_modesl(self):
        result = Guest.objects.get(phone='15306180002')
        self.assertEqual(result.realname, '王斌')
        self.assertTrue(result.sign)


class IndexPageTest(TestCase):

    def test_index_page_renders_index_template(self):
        response = self.client.get('/index/')
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'index.html')


class LoginActionTest(TestCase):

    def setUp(self):
        User.objects.create_user('admin', 'admin@mail.com', '@WSX3edc')

    def test_add_admin(self):
        user = User.objects.get(username='admin')
        self.assertEqual(user.username, 'admin')
        self.assertEqual(user.email, 'admin@mail.com')

    def test_login_action_username_password_null(self):
        test_data = {'username': '', 'password': ''}
        response = self.client.post('/login_action/', data=test_data)
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"username or password error!", response.content)

    def test_login_action_username_pasword_error(self):
        test_data = {'username': 'abc', 'password': '123'}
        response = self.client.post('/login_action/', data=test_data)
        self.assertEqual(response.status_code, 200)
        self.assertIn(b"username or password error!", response.content)

    def test_login_action_success(self):
        test_data = {'username': 'admin', 'password': '@WSX3edc'}
        response = self.client.post('/login_action/', data=test_data)
        self.assertEqual(response.status_code, 302)


class EventManageTest(TestCase):

    def setUp(self):
        User.objects.create_user('admin', 'admin@mail.com', '@WSX3edc')
        Event.objects.create(id=1, name='五一节可否一战？', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Event.objects.create(id=2, name='小龙虾之夜', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        self.login_user = {'username': 'admin', 'password': '@WSX3edc'}
        self.client.post('/login_action/', data=self.login_user)

    def test_event_manage_sucess(self):
        response = self.client.post('/event_manage/')
        self.assertEqual(response.status_code, 200)
        self.assertIn('五一节可否一战？'.encode('utf-8'), response.content)
        self.assertIn('金山度假村石化二村村长寝室'.encode('utf-8'), response.content)

    def test_event_manage_search_success(self):
        response = self.client.post('/search_event/', {"search_event": "五一节可否一战？"})
        self.assertEqual(response.status_code, 200)
        self.assertIn('五一节可否一战？'.encode('utf-8'), response.content)
        self.assertIn('金山度假村石化二村村长寝室'.encode('utf-8'), response.content)

    def test_event_manage_search_fail(self):
        response = self.client.post('/search_event/', {"search_event": "asd"})
        self.assertEqual(response.status_code, 200)
        self.assertNotIn('小龙虾', response.content.decode('utf-8'))
        self.assertNotIn('金山度假村石化二村村长寝室', response.content.decode('utf-8'))


class GuestManageTest(TestCase):

    def setUp(self):
        User.objects.create_user('admin', 'admin@mail.com', '@WSX3edc')
        Event.objects.create(id=1, name='五一节可否一战？', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Event.objects.create(id=2, name='小龙虾之夜', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Guest.objects.create(id=1, event_id=1, realname='王斌', phone='15306180002', email='wangbing@mail.com',
                             sign=False)
        self.login_user = {'username': 'admin', 'password': '@WSX3edc'}
        self.client.post('/login_action/', data=self.login_user)

    def test_event_manage_sucess(self):
        response = self.client.post('/guest_manage/')
        self.assertEqual(response.status_code, 200)
        self.assertIn('王斌'.encode('utf-8'), response.content)
        self.assertIn('15306180002'.encode('utf-8'), response.content)

    def test_event_manage_search_success(self):
        response = self.client.post('/search_guest/', {"search_guest": "王斌"})
        self.assertEqual(response.status_code, 200)
        self.assertIn('王斌'.encode('utf-8'), response.content)
        self.assertIn('15306180002'.encode('utf-8'), response.content)

    def test_event_manage_search_fail(self):
        response = self.client.post('/search_guest/', {"search_guest": "abc"})
        self.assertEqual(response.status_code, 200)
        self.assertNotIn('王斌', response.content.decode('utf-8'))
        self.assertNotIn('15306180002', response.content.decode('utf-8'))


class SignIndexAction(TestCase):

    def setUp(self):
        User.objects.create_user('admin', 'admin@mail.com', '@WSX3edc')
        Event.objects.create(id=1, name='五一节可否一战？', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Event.objects.create(id=2, name='小龙虾之夜', status=False, limit=8, address='金山度假村石化二村村长寝室',
                             start_time='2018-05-01 12:00:00')
        Guest.objects.create(event_id=1, realname='王琪瑞', phone='15306180001', email='wangqirui@mail.com',
                             sign=True)
        Guest.objects.create(event_id=2, realname='王斌', phone='15306180002', email='wangbing@mail.com',
                             sign=False)
        self.login_user = {'username': 'admin', 'password': '@WSX3edc'}
        self.client.post('/login_action/', data=self.login_user)

    def test_sign_index_action_phone_null(self):
        response = self.client.post('/sign_index_action/2/', {"phone": ""})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'phone error', response.content)

    def test_sign_index_action_phone_or_event_id_error(self):
        response = self.client.post('/sign_index_action/1/', {"phone": "15306180002"})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'event id or phone error', response.content)

    def test_sign_index_action_user_has_sign(self):
        response = self.client.post('/sign_index_action/1/', {"phone": "15306180001"})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'user has sign in', response.content)

    def test_sign_index_action_sign_success(self):
        response = self.client.post('/sign_index_action/2/', {"phone": "15306180002"})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'sign in success', response.content)
